package ying.sms;

import java.time.LocalDate;
import java.util.Random;

/**
 * ClassName: StudentManagementSystem
 * Package: sms
 * Description:
 *
 * @Author 刘英杰
 * @Create 2024/5/17 19:12
 * @Version 1.0
 */
public class StudentManagementSystem {
    private Student[] students;

    private int count; // 有效学生数量

    public StudentManagementSystem(int capacity)
    {
        this.students = new Student[capacity];
    }

    /**
     * 需求: 每个学生均有一个入学日期
     * 学生信息管理系统能够查询指定日期之前(包含指定日期)入学的学生信息
     * 学生信息管理系统能够查询指定日期之后(包含指定日期)入学的学生信息
     * @param
     */

    /**
     * 模拟点名，返回随机学生信息
     * @return
     */

    public Student randomRollCall()
    {
        return students[new Random().nextInt(count)];
    }

    /**
     * 校验邮箱
     * @param student
     * @return
     */
    public boolean validateStudentEmai(Student student)
    {
        if (student == null || student.getEmal() == null) return false;
        else
        {
            return student.getEmal().matches("\\w+@\\w+\\.+\\w+");
        }
    }


    public Student[] getStudentBeforeDate(String date)
    {
        Student[] tmps = new Student[students.length];

        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
//            if (student != null && student.getEnrollmentDates() != null &&
//                    student.getEnrollmentDates().isBefore(LocalDate.parse(date)) ||
//                    student.getEnrollmentDates().isEqual(LocalDate.parse(date))) //LocalDate.parse(date)
//                                                                                    //不指定模式就按照默认年-月-日
//                addStudent(tmps,student);
            if (student != null && student.getEnrollmentDates() != null)
            {
                if (student.getEnrollmentDates().isBefore(LocalDate.parse(date)) ||
                    student.getEnrollmentDates().isEqual(LocalDate.parse(date)))
                    addStudent(tmps,student);
            }

        }
        return tmps;
    }

    public Student[] getStudentAfterDate(String date)
    {
        Student[] tmps = new Student[students.length];

        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
//            if (student != null && student.getEnrollmentDates() != null &&
//                    student.getEnrollmentDates().isAfter(LocalDate.parse(date)) ||
//                    student.getEnrollmentDates().isEqual(LocalDate.parse(date))) //LocalDate.parse(date)
//                                                                                    //不指定模式就按照默认年-月-日
//                addStudent(tmps,student);

            if (student != null && student.getEnrollmentDates() != null)
            {
                if (student.getEnrollmentDates().isAfter(LocalDate.parse(date)) ||
                    student.getEnrollmentDates().isEqual(LocalDate.parse(date)))
                    addStudent(tmps,student);
            }
        }
        return tmps;
    }


    /**
     * 查询日期所重载的add
     * @param students
     * @param student
     */
    public void addStudent(Student[] students, Student student)
    {
        if (validateStudentEmai(student))
        {
            for (int i = 0; i < students.length; i++) {
                if (students[i] == null) {
                    students[i] = student;
                    count ++;
                    break;
                }
            }
        }
        else {
            System.out.println("学号为：" + student.getId() + " ,姓名为：" + student.getName() + " ,邮箱为: " +
                    student.getEmal() + " 的学生邮箱格式不符合，添加失败");
        }
    }


    public void addStudent(Student student) {
        addStudent(students,student);
//        if (validateStudentEmai(student)) {
//            for (int i = 0; i < students.length; i++) {
//                if (students[i] == null) {
//                    students[i] = student;
//                    break;
//                }
//            }
//        }
//        else {
//            System.out.println("学号为：" + student.getId() + " ,姓名为：" + student.getName() + ",邮箱为: " +
//                    student.getEmal() + " 的学生邮箱格式不符合，添加失败");
//        }
    }

    /**
     * 重载展示满足日期条件的学生信息
     * @param students
     */
    public void showAllStudent(Student[] students)
    {
        for (int i = 0; i < students.length; i++)
        {
            Student student = students[i];
            if (student != null)
            {
                System.out.println((i + 1) + " " + student.toString());
            }
        }
    }
    public void showAllStudent()
    {
        for (int i = 0; i < students.length; i++)
        {
            Student student = students[i];
            if (student != null)
            {
                System.out.println((i + 1) + " " + student.toString());
            }
        }
    }

    private int getScoreBySubjects(Student student, String subjects) { // ----------------------难点！！！
        switch(subjects)
        {
            case "语文" ->{
                return student.getChineseScore();
            }
            case "数学" ->{
                return student.getMathScore();
            }
            case "历史" ->{
                if (student instanceof MiddleStudents middleStudents)
                    return middleStudents.getHistoryScore();
            }
            case "物理" ->{
                if (student instanceof MiddleStudents middleStudents)
                    return middleStudents.getPhysicsScore();
            }
        }
        return 0;
    }

    public Student getStudentByHighestScore(String subjects)
    {
        int highestScore = 0;
        Student highestScoreStudent = null;
        for (int i = 0; i < students.length; i++)
        {
            Student student = students[i];
            if (student != null)
            {
                int score = getScoreBySubjects(student,subjects);
                if (score > highestScore)
                {
                    highestScore = score;
                    highestScoreStudent = student;
                }
            }
        }
        return highestScoreStudent;
    }

    public Student getStudentByLowestScore(String subjects)
    {
        int lowestScore = Integer.MAX_VALUE;
        Student lowestScoreStudent = null;
        for (int i = 0; i < students.length; i++)
        {
            Student student = students[i];
            if (student != null)
            {
                int score = getScoreBySubjects(student,subjects);
                if (score < lowestScore)
                {
                    lowestScore = score;
                    lowestScoreStudent = student;
                }
            }
        }
        return lowestScoreStudent;
    }

    public int getAverageScore(String subject)
    {
        int sum = 0;
        int cnt = 0;

        for (int i = 0; i < students.length; i++)
        {
            Student student = students[i];
            if (students[i] != null)
            {
                int score = getScoreBySubjects(student,subject);
                sum += score;
                cnt ++;
            }
        }
        return cnt == 0 ? 0 : sum / cnt;
    }

    public void sortByTotalScore()
    {
        for (int i = 0; i < students.length - 1; i++)
        {
            for (int j = 0; j < students.length - i - 1; j++)
            {
                if (students[j] != null && students[j + 1] != null)
                {
                    if (students[j].getTotolScore() < students[j + 1].getTotolScore())
                    {
                        Student tmp = students[j];
                        students[j] = students[j + 1];
                        students[j + 1] = tmp;
                    }
                }
            }
        }
    }

    public Student findStudentById(int id)
    {
        for (int i = 0; i < students.length; i++)
        {
            Student student = students[i];
            if (student != null && student.getId() == id)
                return student;

        }
        return null;
    }


    /*
    *   判断系统中是否存在一个学生（姓名）
    *
     */

    public boolean is_contains(Student stu)
    {
        if (stu == null) return false;

        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (student != null && student.equals(stu))
                return true;
        }
        return false;
    }



}
